package ces.lawsuit.controller;


import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;

import ces.lawsuit.model.ParamValue;
import ces.lawsuit.model.Result;
import ces.lawsuit.util.App;
import ces.lawsuit.util.AppUtil;
import ces.lawsuit.util.Util;

public class ParamValueController extends BaseController<ParamValue> {
	
    public void dataGrid(){
    	String id=getPara(0);
      renderJson(JsonKit.toJson(	ParamValue.me.find(ParamValue.LIST_SQL,id)));
    }
    /**更新参数状态 1:启用;0:停用*/
	public void updateStatus(){
		String id=getPara("id");
		int status=getParaToInt("status");
		int newStatus=status==App.Status.START?App.Status.STOP:App.Status.START;
		Db.update(ParamValue.UPDATE_STATUS_SQL,newStatus,id);
	}
	/**删除,sql 攻击风险*/
	public void delete(){
		String ids=getPara("id");
		if(StrKit.notBlank(ids)){
			Db.update(ParamValue.BATCH_DELETE_SQL.replace("?", ids));
		}
	}
	/**保存*/
	public void save(){
		String paramId=getPara(0);
		List<ParamValue> pvs=getModels(ParamValue.class, "paramValue");
	    for(ParamValue pv:pvs){
	    	pv.set("id", AppUtil.generateId());
	    	pv.set("paramid", paramId);
	  	    pv.save();
	    }
		
	}
	public void saveOrUpdate(){
		//String paramId=getPara(0);
		String paramId=getPara("paramId");
		List<ParamValue> pvs=getModels(ParamValue.class, "paramValue");
	    for(ParamValue pv:pvs){
	    	
	    	if(Util.isNull(pv.get("paramId"))){
	    		if(Util.isNull(paramId)){
	    			paramId=AppUtil.generateId();
	    		}	
	    		if(Util.isNull(pv.get("paramId")))
	    		{pv.set("paramId",paramId);}
	    		
	    	}
	    	if(Util.isNull(pv.get("id"))){
	    		pv.set("id", AppUtil.generateId());
	    		pv.save();
	    	}else{
	    	//pv.set("paramid", paramId);
	  	    pv.update();}
	    }
	     returnMap.put("statusCode", "200");
	     returnMap.put("message", "操作成功!");
	     returnMap.put("paramId", paramId);
	     System.out.println("---"+paramId);
		 renderJson(JsonKit.toJson(returnMap));
	}
}
